package com.sz.mes.service.impl.warehouse;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diboot.core.binding.Binder;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.sz.mes.dto.warehouse.LatticeGoodsDTO;
import com.sz.mes.entity.Goods;
import com.sz.mes.entity.warehouse.LatticeGoods;
import com.sz.mes.mapper.GoodsMapper;
import com.sz.mes.mapper.warehouse.LatticeGoodsMapper;
import com.sz.mes.service.warehouse.LatticeGoodsService;
import com.sz.mes.service.impl.BaseCustomServiceImpl;
import com.sz.mes.vo.warehouse.LatticeGoodsDetailVO;
import com.sz.mes.vo.warehouse.LatticeGoodsListVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.record.DVALRecord;
import org.hibernate.validator.constraints.Length;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 货格对应商品表相关Service实现
 *
 * @author hem
 * @version 1.0
 * @date 2023-05-25
 * Copyright © szhs
 */
@Service
@Slf4j
public class LatticeGoodsServiceImpl extends BaseCustomServiceImpl<LatticeGoodsMapper, LatticeGoods> implements LatticeGoodsService {
    private final LatticeGoodsMapper latticeGoodsMapper;
    private final GoodsMapper goodsMapper;

    public LatticeGoodsServiceImpl(LatticeGoodsMapper latticeGoodsMapper, GoodsMapper goodsMapper) {
        this.latticeGoodsMapper = latticeGoodsMapper;
        this.goodsMapper = goodsMapper;
    }

    @Override
    public JsonResult getCommodityInventory(LatticeGoodsDTO queryDto, Pagination pagination) {
//        Page<LatticeGoods> pageArgs = new Page(pagination.getPageIndex(), pagination.getPageSize());
//        QueryWrapper<LatticeGoods> wrapper = new QueryWrapper<>();
//
//        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
//        if (!StringUtils.isEmpty(queryDto.getGoodsCode())) {
//            wrapper.like("goods_code", queryDto.getGoodsCode());
//        } else if (!StringUtils.isEmpty(queryDto.getGoodsName())) {
//            wrapper.like("goods_name", queryDto.getGoodsName());
//        }

        /*if (queryDto.getGoodsName() == null && queryDto.getGoodsCode() == null) {

        } else {
            List<Goods> goods = goodsMapper.selectList(queryWrapper);
            List<Long> ids = goods.stream().map(Goods::getId).collect(Collectors.toList());
            if (ids.size() > 0) {
                wrapper.in("goods_id", ids);
            } else {
                return JsonResult.OK(null);
            }
        }*/
//        wrapper.orderByAsc("lable");
//        wrapper.ne("quantity", 0);

        List<LatticeGoods> allLatticeGoods = latticeGoodsMapper.selectLatticeGoodsByAll(queryDto.getGoodsName(), queryDto.getGoodsCode());
        List<LatticeGoods> latticeGoods = latticeGoodsMapper.selectLatticeGoods((pagination.getPageIndex() - 1) * pagination.getPageSize(),
                pagination.getPageSize(), queryDto.getGoodsName(), queryDto.getGoodsCode());

//        Page<LatticeGoods> latticeGoodsPage = latticeGoodsMapper.selectPage(pageArgs, wrapper);
//        pagination.setTotalCount(latticeGoodsPage.getTotal());
        pagination.setTotalCount(allLatticeGoods.size());
        List<LatticeGoodsDetailVO> latticeGoodsDetailVOS = Binder.convertAndBindRelations(latticeGoods, LatticeGoodsDetailVO.class);
//        List<LatticeGoodsDetailVO> latticeGoodsDetailVOS = Binder.convertAndBindRelations(latticeGoodsPage.getRecords(), LatticeGoodsDetailVO.class);
        //倒叙排列
//        List<LatticeGoodsDetailVO> reversedList = latticeGoodsDetailVOS.stream().
//        sorted(Comparator.comparing(LatticeGoods::getCreateTime).reversed()).collect(Collectors.toList());
        return JsonResult.OK(latticeGoodsDetailVOS).bindPagination(pagination);
    }

    @Override
    public JsonResult getLatticeGoodsById(Long id) {
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<LatticeGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warehouse_lattice_id", id);
        List<LatticeGoods> latticeGoods = latticeGoodsMapper.selectList(queryWrapper);
        map.put("data", latticeGoods);
        return JsonResult.OK(map);
    }

    @Override
    public JsonResult getList(String goodCode) {
        QueryWrapper<LatticeGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_code", goodCode);
        List<LatticeGoods> latticeGoodsList = latticeGoodsMapper.selectList(queryWrapper);
        return JsonResult.OK(latticeGoodsList);
    }
//        wrapper.orderByAsc("lable");
//        wrapper.ne("quantity", 0);
//        Page<LatticeGoods> latticeGoodsPage = latticeGoodsMapper.selectPage(pageArgs, wrapper);
//        pagination.setTotalCount(latticeGoodsPage.getTotal());
//        List<LatticeGoodsDetailVO> latticeGoodsDetailVOS = Binder.convertAndBindRelations(latticeGoodsPage.getRecords(), LatticeGoodsDetailVO.class);
//        return JsonResult.OK(latticeGoodsDetailVOS).bindPagination(pagination);


}
